package com.ruoyi.controller.system;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.vo.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.controller.BaseController;
import com.ruoyi.domain.SysMenu;
import com.ruoyi.service.SysMenuService;

@RestController
@RequestMapping("system/menu")
public class SysMenuController extends BaseController{
	
	@Autowired
	private SysMenuService sysMenuService;
	
	/**
	 * 获取菜单列表
	 * @param menu
	 * @return
	 */
	@GetMapping("list")
	public AjaxResult list(SysMenu menu) {
		logger.info("获取菜单列表,menu:{}",menu);
		List<SysMenu> list = sysMenuService.selectMenuList(menu,getUserId());
		return AjaxResult.success(list);
	}
	
	/**
	 * 根据菜单编号获取信息
	 * @param menuId
	 * @return
	 */
	@GetMapping(value = "/{menuId}")
	public AjaxResult getInfo(@PathVariable Long menuId) {
		logger.info("根据菜单编号获取信息,menuId:{}",menuId);
		return AjaxResult.success(sysMenuService.selectMenuById(menuId));
	}
	
	/**
	 * 获取菜单下拉树
	 * @param menu
	 * @return
	 */
	@GetMapping("/treeselect")
	public AjaxResult treeselect(SysMenu menu) {
		logger.info("获取菜单下拉树,menu:{}",menu);
		List<SysMenu> menus = sysMenuService.selectMenuList(menu,getUserId());
		return AjaxResult.success(sysMenuService.buildMenuTreeSelect(menus));
	}
	
	/**
	 * 加载对应角色菜单列表树
	 * @param roleId
	 * @return
	 */
	@GetMapping(value = "/roleMenuTreeselect/{roleId}")
	public AjaxResult roleMenuTreeselect(@PathVariable("roleId") Long roleId) {
		logger.info("加载对应角色菜单列表树,roleId:{}",roleId);
		List<SysMenu> menus = sysMenuService.selectMenuList(getUserId());
		AjaxResult ajax = AjaxResult.success();
		ajax.put("checkedKeys", sysMenuService.selectMenuListByRoleId(roleId));
		ajax.put("menus", sysMenuService.buildMenuTreeSelect(menus));
		return ajax;
	}
	
	/**
	 * 新增菜单
	 * @param menu
	 * @return
	 */
	@PostMapping
	public AjaxResult add(@Validated @RequestBody SysMenu menu) {
		logger.info("新增菜单,menu:{}",menu);
		if (!sysMenuService.checkMenuNameUnique(menu)) {
			return error("新增菜单" + menu.getMenuName() + "失败，菜单名称已存在");
		} else if (UserConstants.YES_FRAME.equals(menu.getIsFrame()) && !StringUtils.ishttp(menu.getPath())) {
			return error("新增菜单" + menu.getMenuName() + "失败，地址要以http://开头");
		}
		menu.setCreateBy(getUsername());
		return toAjax(sysMenuService.insertMenu(menu));
		
	}
	
	/**
	 * 修改菜单
	 * @param menu
	 * @return
	 */
	@PutMapping
	public AjaxResult edit(@Validated @RequestBody SysMenu menu) {
		logger.info("修改菜单,menu:{}",menu);
		if (!sysMenuService.checkMenuNameUnique(menu)) {
			return error("修改菜单" + menu.getMenuName() + "失败，菜单名称已存在");
		} else if (UserConstants.YES_FRAME.equals(menu.getIsFrame()) && !StringUtils.ishttp(menu.getPath())) {
			return error("修改菜单" + menu.getMenuName() + "失败，地址要以http://开头");
		} else if (menu.getMenuId().equals(menu.getParentId())) {
			return error("修改菜单'" + menu.getMenuName() + "'失败，上级菜单不能选择自己");
		}
		menu.setUpdateBy(getUsername());;
		return toAjax(sysMenuService.updateMenu(menu));
		
	}
	
	/**
	 * 删除菜单
	 * @param menuId
	 * @return
	 */
	@DeleteMapping("/{menuId}")
	public AjaxResult remove(@PathVariable Long menuId) {
		logger.info("删除菜单,menuId:{}",menuId);
		if (sysMenuService.hasChildByMenuId(menuId)) {
			return warn("存在子菜单，不允许删除");
		}
		return toAjax(sysMenuService.deleteMenuById(menuId));
	}
	
}
